 

import Control.*;
import Model.*;

/**
 * UI for Employees
 * @author Stefan
 */
public class EmployeesUI {

    private AssociateController assCtr = AssociateController.getInstance();

    /**
     * Start menu!
     */
    public void start()
    {
        employeesMenu();
    }

    /**
     * Employee menu
     */
    private void employeesMenu()
    {
        boolean exit = false;
        while (!exit)
        {
            int choise = writeEmployeesMenu();
            if (choise == 1)
            {
                startCreate(CommonUIFunctions.readString("\n write ID: "), CommonUIFunctions.readString("\n write name: "), CommonUIFunctions.readString("\n write address: "), CommonUIFunctions.readString("\n write department: "));
            }
            else if (choise == 2)
            {
                startRead(CommonUIFunctions.readString("\n write Id of the Employee you want to find "));
            }
            else if (choise == 3)
            {
                startUpdate(CommonUIFunctions.readString("\n write the id of Employee update: "));
            }
            else if (choise == 4)
            {
                startDelete(CommonUIFunctions.readString("\n write Id of the Employee you want to delete: "));
            }
            else
            {
                exit = true;
            }
        }
    }

    /*
     * Write Employee menu
     */
    private int writeEmployeesMenu()
    {
        System.out.println("\f *** Employees menu ***");
        System.out.println(" (1) Create");
        System.out.println(" (2) Read");
        System.out.println(" (3) Update");
        System.out.println(" (4) Delete");
        System.out.println(" (0) Back");

        return CommonUIFunctions.readInt("\n Make your choise: ");
    }

    /*
     * create menu
     */
    private void startCreate(String id, String name, String address, String company)
    {
        if (!assCtr.checkExistingEmployeeID(id))
        {
            assCtr.addEmployee(id, name, address, company);
        }
        else
        {
            startCreate(CommonUIFunctions.readString("Employee ID is already in use, choose another: "), name, address, company);
        }
    }

    /*
     * Start read menu
     */
    private void startRead(String id)
    {
        try
        {
            getInfo(id);
        }
        catch (Exception e)
        {
            System.out.println("There was no user with this ID");
        }
    }

    /*
     * Start delete menu
     */
    private void startDelete(String id)
    {
        if (assCtr.deleteAssociateID(id) == false)
        {
            System.out.println("There was no user with this ID");
        }
    }

    /**
     * Start update menu
     * @param id
     */
    private void startUpdate(String id)
    {
        try
        {
            Employee emp = getInfo(id);
            updateMenu(emp);
        }
        catch (Exception e)
        {
            System.out.println("There was no user with this ID");
        }
    }

    /*
     * Update menu
     */
    private void updateMenu(Employee emp)
    {
        boolean exit = false;
        while (!exit)
        {
            int choise = writeUpdateMenu();
            if (choise == 1)
            {
                String newID = CommonUIFunctions.readString("\n write new ID: ");
                while(assCtr.checkExistingEmployeeID(newID))
                {
                    newID = CommonUIFunctions.readString("\n Employee ID is already in use, choose another: ");
                }
                emp.setId(newID);
            }
            else if (choise == 2)
            {
                emp.setName(CommonUIFunctions.readString("\n write new name: "));
            }
            else if (choise == 3)
            {
                emp.setAddress(CommonUIFunctions.readString("\n write new address: "));
            }
            else if (choise == 4)
            {
                emp.setCompany(CommonUIFunctions.readString("\n write new department: "));
            }
            else
            {
                exit = true;
            }
        }
    }

    /*
     * Write update menu
     */
    private int writeUpdateMenu()
    {
        System.out.println("\f *** UpdateMenu ***");
        System.out.println("\f Choose one to edit");
        System.out.println(" (1) Id ");
        System.out.println(" (2) Name");
        System.out.println(" (3) Address");
        System.out.println(" (4) Department");
        System.out.println(" (0) Back");

        return CommonUIFunctions.readInt("\n Make your choise: ");
    }

    /*
     * Get info about Employee
     */
    private Employee getInfo(String id)
    {
        Employee emp = assCtr.findEmployeeID(id);
        System.out.println("Employee \n" + "Id: " + emp.getId() + "\nName: " + emp.getName() + "\nDepartment: " + emp.getCompany() + "\nAddress: " + emp.getAddress());
        return emp;
    }
}
